#include"lars_api.h"
#include<map>

// 定义一个map，用于存放成功和失败的次数
// key-->ip     value--><succ_cnt, err_cnt>
typedef std::map<std::string, std::map<int, std::pair<int, int>>> map;
typedef map::iterator map_it;

typedef std::map<int, std::pair<int, int>> port_map;
typedef port_map::iterator port_map_it;

// 开发者业务模块
int main(int argc, char* argv[]){

    if(argc < 3){
        printf("usage: ./simlator [modid] [cmdid] [errRate(0-100)] [query cnt(0-9999)]\n");
        return 1;
    }

    int modid = atoi(argv[1]);
    int cmdid = atoi(argv[2]);

    int err_rate = 20;   // 20%
    int query_cnt = 100;

    if(argc > 3){
        err_rate = atoi(argv[3]);
    }
    
    if(argc > 4){
        query_cnt = atoi(argv[4]);
    }
    printf("new modid:%d, cmdid:%d, errRate:%.2f, query_cnt:%d\n", modid, cmdid, err_rate/100.0, query_cnt);

    lars_client client;
    int ret;
    // 输出的IP和port
    std::string ip;
    int port;
  
    map results;
    ret = client.reg_init(modid, cmdid);
    if(ret == 0){

        for(int i=0; i<query_cnt; i++){
            ret = client.get_host(modid, cmdid, ip, port);
            if(ret == 0){
                
                // 获得IP和port，根据IP和port做业务
                if(results.find(ip) == results.end()){
                    // 首次获取成功
                    std::pair<int, int> succ_err(0, 0);
                    port_map p_map;
                    p_map[port] = succ_err;
                    results[ip] = p_map;
                }
                
                if(rand()%100 < err_rate){
                    // 认为这是错误的消息
                    results[ip][port].second++;
                    // 根据之前之前host使用的情况上报对应的host情况
                    client.report_host(modid, cmdid, ip, port, 1);
                    printf("overload! host data : ip:%s port:%d\n", ip.c_str(), port);
                }
                else{
                    // 认为是正确的消息
                    results[ip][port].first++;
                    // 根据之前之前host使用的情况上报对应的host情况
                    client.report_host(modid, cmdid, ip, port, 0);
                    printf("success! host data : ip:%s port:%d\n", ip.c_str(), port);
                }
            
            }
            else if(ret == 1){
                fprintf(stderr, "all hosts is overload retcode:%d\n",ret);
                // 认为这是错误的消息
                results[ip][port].second++;
            }
            else if(ret == 2){
                fprintf(stderr, "system error retcode:%d\n",ret);
                // 认为这是错误的消息
                results[ip][port].second++;
            }
            else if(ret == 3){
                fprintf(stderr, "data is not exist retcode:%d\n",ret);
            }

            usleep(10000);
            
        }
    
        for(map_it it=results.begin(); it!=results.end(); it++){
            for(port_map_it pit=it->second.begin(); pit!= it->second.end(); pit++){
                printf("ip:%s port:%d sum succ:%d sum err:%d\n", it->first.c_str(), pit->first, pit->second.first, pit->second.second);
            }
        }
    
    }
    else if(ret == 2){
        fprintf(stderr, "system error:%d\n",ret);
    }
    else if(ret == 3){
        fprintf(stderr, "data is not exist:%d\n",ret);
    }
    
    return 0;
}
